<?php
namespace Admin\Controller;
use Admin\Model\configModel;

use Admin\Model\smsModel;

use Admin\Model\UserModel;
use Think\Controller;
class BaseController extends Controller {
     public $_APPID = '';
	 public $_AppSecret= '';
	 public $_userId = '';
	 public $_userName = '';
	 public $_userAuth = '';
	 public $_belong = '';
	 public $_openid='';
	
    public function __construct(){
    	parent::__construct();
	 	$this->_APPID =C('APPID');
	 	$this->_AppSecret = C('AppSecret');
		$userModel = new UserModel();
    	if ($_COOKIE['sessionid'] && $userResult = $userModel->getUserItem(array('isused'=>1,'sessionid'=>$_COOKIE['sessionid']))) {
         	$this->_userId = $userResult['userid'];
            $this->_userName = $userResult['nickname'];
            $this->_userAuth = $userResult['userauth'];
            $this->_belong = $userResult['belongboss'];
            $this->_openid = $userResult['openid'];
            $cookie = array('sessionid' => $_COOKIE['sessionid']);
        	$this->ssetcookie($cookie, 60*60*24*7, '/');
         }
         else{
         	$this->_userId = '';
            $this->_userName = '';
            $this->_userAuth = '';
            $this->_belong = '';
         	$cookie = array('sessionid' => '');
        	$this->ssetcookie($cookie, -1, '/');
         }
    }


	/**
	 * 菜单列表
	 * @return \Home\Model\unknown
	 */
	public function getmenuList() {
		$menuModel = new menuModel();
		//主菜单列表
        $menuauth= $this->_userAuth;
        $menuList = $menuModel->getMainMenuList($menuauth);
		
		foreach ($menuList as $key=> $value) {
			$sidebardata['menukey'] = $value['menukey'];
			$sidebar = $menuModel->getSubMenuList($sidebardata);
			foreach($sidebar as $v){
				$menuList[$key]['selected'] = 2;//未选中
	    		if(strpos($_SERVER['REQUEST_URI'],$v['menuurl'])){
	    			$menuList[$key]['selected'] = 1;//选中
	    			break;
	    		}
	    	}
	    	$menuList[$key]['menuurl'] = U($value['menuurl']);
    	}
        return $menuList;
	}
    
	/**
	* 验证手机号是否正确
	* @author 
	* @param INT $mobile
	*/
	 function isMobile($mobile) {
	    if (!is_numeric($mobile)) {
	        return false;
	    }
	    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
	 }
		
	/**
	 * 短信验证是否有效
	 * @param unknown_type $tel
	 * @param unknown_type $verification
	 * @return multitype:number string 
	 */
	public function smsValidTime($tel,$verification) {
		$smsModel = new smsModel();
		$configModel = new configModel();
		//短信有效时间
		$dataConfig['id']=4;
		$resultConfig = $configModel->getConfigItem($dataConfig);
		$time = intval($resultConfig['extra']);
		
		$result=array('state'=>1,'msg'=>'验证码成功');
		
		$smsData['tel']=$tel;
		$between =(time()-$time).",". time();
		$smsData['createunixtime'] =array('between',$between);
    	$isExistSmsData = $smsModel->getSmsItem($smsData);
		if($isExistSmsData ===false || empty($isExistSmsData)){
			$result=array('state'=>-1,'msg'=>'验证码已失效，请重新申请');
		}
		if($isExistSmsData['verification'] != $verification){
			$result=array('state'=>-1,'msg'=>'验证码不正确，请确认');
		}
		
		return $result;
	}
	
	/**
	 * 获取两位随机字母
	 */
	function getRandChar($length){
	    $str = null;
	    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	    $max = strlen($strPol)-1;
	
	    for($i=0;$i<$length;$i++){
	        $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
	    }
	
	    return $str;
	}
    
	/**
	 * cookie设置
	 * @param $var 设置的cookie名
	 * @param $value 设置的cookie值
	 * @param $life 设置的过期时间：为整型，单位秒 如60表示60秒后过期
	 * @param $path 设置的cookie作用路径
	 * @param $domain 设置的cookie作用域名
	 */
	public static function ssetcookie($array, $life=0, $path = '/') {
		$_cookName_ary = array_keys($array);
		for($i = 0; $i < count($array); $i++) {
			//echo $_cookName_ary[$i].'='.$array[$_cookName_ary[$i]].'<br>';
			$httpOnly = false;
			setcookie($_cookName_ary[$i], $array[$_cookName_ary[$i]], $life?(time()+$life):0, $path);
		}
	}

	
	
}

